﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Bll
{
    public class AdminAuthorizeAttribute : AuthorizeAttribute
    {
        private string Power;

        /// <summary>
        /// 初始函数
        /// </summary>
        /// <param name="powerstr"></param>
        public AdminAuthorizeAttribute(string powerstr, int AdmColID = 0, byte ClassLevel = 0, string Url = "")
        {
            Power = AdmColService.Get(powerstr, classLevel: ClassLevel, admColID: AdmColID, url: Url);
        }

        private static AdmInfo current = AdmInfo.Instance;

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //            return true;
            if (httpContext == null)
                throw new ArgumentNullException("httpContext");
            if (!current.IsAuthenticated)
            {
                httpContext.Response.Redirect("~/adm/AdmCol/Login");
                return false;
            }
            if (current.AdmRole == DAL.Enums.emAdmRole.SysManage.Value) //超级管理员
            {
                return true;
            }
            if (string.IsNullOrWhiteSpace(current.ColKeys) || !current.ColKeys.Contains("{" + Power + "}"))
            {
                httpContext.Response.Redirect("~/adm/AdmCol/Main");
                return false;
            }
            return true;
        }

    }
}